package com.template.mongo.model;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;

/**
 * Model for GenericMongoBaseDao
 * @author secret
 */
@SuppressWarnings({ "serial", "unchecked" })
public abstract class BaseBean implements Serializable, Cloneable {

        @Id
        protected ObjectId id;

        public BaseBean() {
                this.id = null;
        }

        public ObjectId getId() {
                return id;
        }

        public void setId(ObjectId id) {
                this.id = id;
        }

        public abstract String getKeyName();

        public abstract Object getKeyValue();

        public abstract void setKeyValue(Object key);

        final public Map<String, Object> toMap() {
                try {
                        Map<String, Object> rs = (Map<String, Object>) PropertyUtils
                                        .describe(this);
                        rs.remove("class");
                        rs.remove("id");
                        rs.remove("keyValue");
                        rs.remove("keyName");
                        return rs;
                } catch (IllegalAccessException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (InvocationTargetException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                } catch (NoSuchMethodException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }
                throw new RuntimeException(this.getClass().getName() + "Conversion map exception occurs!");
        }

}
